package com.easyse.serviceparticaltasks.service.impl;

import com.easyse.model.DO.Task;
import com.easyse.model.DO.User;
import com.easyse.model.vo.ResultVO;
import com.easyse.serviceparticaltasks.mapper.TaskMapper;
import com.easyse.serviceparticaltasks.mapper.UserMapper;
import com.easyse.serviceparticaltasks.service.MissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author ：rc
 * @date ：Created in 2022/10/18 9:33
 * @description：
 */
@Service
public class MissionServiceImpl implements MissionService {

    @Autowired
    TaskMapper taskMapper;

    @Override
    public ResultVO getLaterMission() {
        // TODO getLaterMission
        return null;
    }

    @Autowired
    UserMapper userMapper;
    @Override
    public ResultVO getMissionList(Integer groupID) {
        ResultVO resultVO;
        HashMap<String,Object> params = new HashMap<>();
        params.put("group_Id",groupID);
        List<Task> tasks = taskMapper.selectByMap(params);
        if(tasks.isEmpty()){
            resultVO = ResultVO.failure("任务信息为空！");
        }else{
            resultVO = ResultVO.success("获取任务列表成功！");
            HashMap<String,Object> data = new HashMap<>();
            data.put("missions",tasks);
            resultVO.setData(data);
        }
        return resultVO;
    }

    @Override
    public ResultVO addMission(Task task) {
        ResultVO resultVO;
        int insert = taskMapper.insert(task);
        if(insert == 1){
            resultVO = ResultVO.success("任务添加成功");
        }else{
            resultVO = ResultVO.failure("发生未知错误，任务添加失败！");
        }

        return resultVO;
    }

    @Override
    public ResultVO getStudentMission(Integer userId) {
        ResultVO resultVO;
        //查出学生信息 拿到小组号和姓名
        User user = userMapper.selectById(userId);
        String username = user.getUsername();
        Long groupId = user.getGroupId();
        //通过姓名和小组号查出任务
        HashMap<String,Object> params = new HashMap<>();
        params.put("group_ID",groupId);
        params.put("assignedTO",username);
        List<Task> tasks = taskMapper.selectByMap(params);
        if(tasks.isEmpty()){
            resultVO = ResultVO.failure("任务为空！");
        }else{
            resultVO = ResultVO.success("获取任务信息成功!");
            HashMap<String,Object> data = new HashMap<>();
            data.put("missions",tasks);
            resultVO.setData(data);
        }
        return resultVO;
    }

    @Override
    public ResultVO updateMission(Task task) {
        ResultVO resultVO = null;
        Integer id = task.getId();
        if(ObjectUtils.isEmpty(id)){
            resultVO = ResultVO.failure("发生错误，更新任务失败！");
        }else{
            int i = taskMapper.updateById(task);
            if(i == 1){
                resultVO = ResultVO.success("更新任务成功!");
            }
        }
        return resultVO;
    }

    @Override
    public ResultVO endMission(String way, Integer userID, Integer taskID, String stroy) {
        //TODO task添加任务返回的信息

        ResultVO resultVO = null;
        //任务结束 任务完成
        Integer i;
        if("finish".equals(way)){
            Task task = taskMapper.selectById(taskID);
            task.setFinisheddate(new Date());
            task.setMailto(stroy);
            String username = userMapper.selectById(userID).getUsername();
            task.setFinishedby(username);
            //更新任务
            i = taskMapper.updateById(task);
            if(i == 1){
                resultVO = ResultVO.success("任务成功结束！");
            }else{
                resultVO = ResultVO.failure("任务结束失败!");
            }

        }else if("close".equals(way)){
            //任务的时间到了 任务被关闭
            Task task = taskMapper.selectById(taskID);
            task.setCloseddate(new Date());
            String username = userMapper.selectById(userID).getUsername();
            task.setClosedby(username);
            //更新任务
            i = taskMapper.updateById(task);
            if(i == 1){
                resultVO = ResultVO.success("任务关闭成功！");
            }else{
                resultVO = ResultVO.failure("任务关闭失败!");
            }
        }else if("cancle".equals(way)){
            //任务被取消
            Task task = taskMapper.selectById(taskID);
            task.setCanceleddate(new Date());
            String username = userMapper.selectById(userID).getUsername();
            task.setCanceledby(username);
            //更新任务
            i = taskMapper.updateById(task);
            if(i == 1){
                resultVO = ResultVO.success("任务已取消！");
            }else{
                resultVO = ResultVO.failure("任务取消失败!");
            }
        }

        return resultVO;
    }


}
